<?php

namespace catchAdmin\basics\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catcher\CatchAuth;
use catchAdmin\basics\model\BusinessType as BusinessTypeModel;
use catchAdmin\basics\request\BusinessTypeCreateRequest;
class BusinessType extends CatchController
{

    protected $BusinessTypeModel;

    /**
     *
     * @time 2022/02/28 16:55
     * @param BusinessTypeModel $BusinessTypeModel
     * @return mixed
     */
    public function __construct(BusinessTypeModel $BusinessTypeModel)
    {
        $this->BusinessTypeModel = $BusinessTypeModel;
    }

    /**
     *
     * @time 2022/02/15 11:41
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function index(CatchAuth $auth)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->BusinessTypeModel->getList($user['tenant_id']));
    }

    /**
     *
     * @time 2022/02/15 11:41
     * @param CatchAuth $auth
     * @param BusinessTypeCreateRequest $request
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function create(CatchAuth $auth,BusinessTypeCreateRequest $request)
    {
        $params = $request->filterEmptyField()->post();
        $user = $auth->user();
        $params['tenant_id'] = $user['tenant_id'];
        return CatchResponse::success($this->BusinessTypeModel->storeBy($params),"新建成功");
    }

    /**
     *
     * @time 2022/02/28 16:55
     * @param BusinessTypeCreateRequest $request
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function update(BusinessTypeCreateRequest $request)
    {
        $params = $request->post();
        return CatchResponse::success($this->BusinessTypeModel->updateBy($params['id'], $params),"编辑成功");
    }

    /**
     * 删除
     * @time 2022/02/28 15:16
     * @param Request $request
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
         $id = $request->post('id');
         return CatchResponse::success($this->BusinessTypeModel->deleteBy($id),'删除成功');
    }

    /**
     * 查询全部
     * @time 2022/03/18 15:16
     * @return \think\response\Json
     */
    public function getBusinessType()
    {
         $data = $this->BusinessTypeModel->where('deleted_at','=',0)->field('name,abbreviation')->select();
         return CatchResponse::success($data,'查询成功');
    }
}